package com.cskaoyan.catmall.controller.background;

import com.cskaoyan.catmall.model.bo.PromotionAdBo;
import com.cskaoyan.catmall.model.pojo.MarketAd;
import com.cskaoyan.catmall.model.vo.BaseRespVo;
import com.cskaoyan.catmall.model.vo.ItemOfAdListVo;
import com.cskaoyan.catmall.model.vo.PromotionBaseVo;
import com.cskaoyan.catmall.service.PromotionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 创建日期: 2022-01-06-22-35
 * author : LL
 * description: 推广管理相关接口
 */

@RequestMapping("admin/ad")
@RestController
public class PromotionAdController {


    @Autowired
    PromotionService promotionService;

    /**
     * @param 推广管理参数，除基本包含页码及排序的参数外，还包含广告名称和评论
     * @return 推广管理返回值，其中列表为广告列表
     * @description 推广管理广告
     * @author LL
     * @date 2022/1/7
     */
//    @RequiresPermissions("admin:ad:list")
    @GetMapping("list")
    public BaseRespVo getPromotionAd(PromotionAdBo promotionAd) {
        PageHelper.startPage(promotionAd.getPage(), promotionAd.getLimit());
        PromotionBaseVo promotionAdVo = promotionService.getPromotionAds(promotionAd);
        PageInfo<ItemOfAdListVo> pageInfo = new PageInfo<>(promotionAdVo.getList());
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        promotionAdVo.setLimit(promotionAd.getLimit());
        promotionAdVo.setPages(pages);
        promotionAdVo.setPage(promotionAd.getPage());
        promotionAdVo.setTotal((int) total);
        return BaseRespVo.ok(promotionAdVo);
    }


    /**
     * @param
     * @return
     * @description 图片上传
     * @author LL
     * @date 2022/1/7
     */
//    @PostMapping("storage/create")
//    public BaseRespVo storeImage(@RequestBody MultipartFile file, HttpServletRequest request) throws IOException {
//        String name = file.getOriginalFilename();
//        String key = Integer.toHexString(name.hashCode());
//        MarketStorage marketStorage = new MarketStorage();
//        marketStorage.setAddTime(new Date());
//        marketStorage.setName(name);
//        marketStorage.setUpdateTime(new Date());
//        marketStorage.setSize((int) file.getSize());
//        marketStorage.setType(file.getContentType());
//        marketStorage.setKey(key);
//        String hostAddress = InetAddress.getLocalHost().getHostAddress();
//        int port = request.getLocalPort();
//        String basePath = "wx/storage/image/" + file.getOriginalFilename();
//        marketStorage.setUrl("http://" + hostAddress + ":" + String.valueOf(port) + "/" + basePath);
//        System.out.println("http://" + hostAddress + ":" + String.valueOf(port) + "/" + basePath);
//        File storeFile = new File(filePath + basePath);
//        storeFile.mkdirs();
//        System.out.println(filePath);
//        promotionService.storeImage(marketStorage);
//        file.transferTo(storeFile);
//        return BaseRespVo.ok(marketStorage);
//    }

    /**
     * @param
     * @return
     * @description 添加广告
     * @author LL
     * @date 2022/1/7
     */
    @RequiresPermissions("admin:ad:create")
    @PostMapping("create")
    public BaseRespVo insertPromotionAd(@RequestBody MarketAd marketAd) {
        promotionService.insertAd(marketAd);
        return BaseRespVo.ok(marketAd);
    }

    /**
     * @param
     * @return
     * @description 删除广告
     * @author LL
     * @date 2022/1/7
     */
    @RequiresPermissions("admin:ad:delete")
    @PostMapping("delete")
    public BaseRespVo deletePromotionAd(@RequestBody MarketAd marketAd) {
        promotionService.deleteAd(marketAd);
        return BaseRespVo.ok();
    }

    /**
     * @param
     * @return
     * @description 更新广告
     * @author LL
     * @date 2022/1/7
     */
    @RequiresPermissions("admin:ad:update")
    @PostMapping("update")
    public BaseRespVo updatePromotionAd(@RequestBody MarketAd marketAd) {
        promotionService.updateAd(marketAd);
        return BaseRespVo.ok(marketAd);
    }







//    /**
//     * @param pid=0表示为父类，deletednum=0表示未删除，用来防止sql注入
//     * @return 推广管理返回值，其中列表为分类列表
//     * @description 推广管理优惠券中的分类列表，包含父分类和其中的子分类
//     * @author LL
//     * @date 2022/1/7
//     */
//    @GetMapping("category/list")
//    public BaseRespVo getPromotionCategory(BaseParamBo baseParam) {
//        List<ItemOfPromotionCategoryVo> categoryVos = promotionService.getPromotionCategory(baseParam);
//        return BaseRespVo.ok(categoryVos);
//    }

    /**
     * @param deletednum=0表示未删除，用来防止sql注入
     * @return 推广管理返回值，其中列表为商品列表
     * @description 推广管理优惠券中的商品分类
     * @author LL
     * @date 2022/1/7
     */
//    @GetMapping("goods/list")
//    public BaseRespVo getPromotionGoods(BaseParamBo baseParam) {
//        PageHelper.startPage(baseParam.getPage(),baseParam.getLimit());
//        List<PromotionGoodsListVo> promotionGoodsListVos = promotionService.getPromotionGoods(baseParam);
//        PageInfo<PromotionGoodsListVo> pageInfo = new PageInfo<>(promotionGoodsListVos);
//        long total = pageInfo.getTotal();
//        int pages = pageInfo.getPages();
//        PromotionBaseVo<PromotionGoodsListVo> promotionBaseVo = new PromotionBaseVo<>();
//        promotionBaseVo.setPages(pages);
//        promotionBaseVo.setTotal((int) total);
//        promotionBaseVo.setLimit(baseParam.getLimit());
//        promotionBaseVo.setList(promotionGoodsListVos);
//        promotionBaseVo.setPage(baseParam.getPage());
//        return BaseRespVo.ok(promotionBaseVo);
//    }





}